package cn.ococci.dm.Main;

import com.alibaba.fastjson.Json;
import com.alibaba.fastjson.JsonArray;
import com.alibaba.fastjson.JsonObject;
import com.loopj.android.http.RequestParams;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.ococci.dm.Main.Model.CrashData;
import cn.ococci.dm.Main.Model.DoorModel;
import cn.ococci.dm.Main.Model.Ota;
import cn.ococci.dm.Main.Model.UnitModel;
import cn.ococci.dm.Main.Model.User;
import cn.ococci.dm.Main.Model.WifiObj;
import cn.ococci.dm.Main.Tools.DateTool;
import cn.ococci.dm.Main.Tools.StringTool;

/**
 * Created by cyan on 15/9/25.
 *  数据转换
 */
public class Convert {




    public static User jsonToUser(JsonObject json, String mobile) {
        User user = new User();

        user.setServerId(json.getLong(Const.BuilderParams.user_id));
        user.setPhoneMacId(json.getString(Const.BuilderParams.mac_id));
        user.setName(json.getString(Const.BuilderParams.name));
        user.setCompanyId(json.getLong(Const.BuilderParams.company_id));
        user.setPhone(mobile);

        return user;
    }



    public static List<DoorModel> jsonToDoorList(JsonObject jsonObject){

        List<DoorModel> doorList = new ArrayList<>();
        JsonArray jsonArray = jsonObject.getJSONArray(Const.BuilderParams.detail);

        int count =  jsonArray.size();
        if (count > 0 ){

            for(int i = 0 ;i < count;i++){
                DoorModel door = jsonToDoor(jsonArray.getJSONObject(i));
                doorList.add(door);
            }
            //save to db
            DataSupport.saveAll(doorList);
        }

        return doorList;
    }

    public static DoorModel jsonToDoor(JsonObject jsonObject){
        DoorModel door = new DoorModel();

        door.setDoorMac(jsonObject.getString(Const.BuilderParams.mac));
        door.setDoorName(jsonObject.getString(Const.BuilderParams.name));
        door.setDoorId(jsonObject.getIntValue(Const.BuilderParams.door_id));
        door.setWifiBssid(jsonObject.getString(Const.BuilderParams.mac));

        if (jsonObject.containsKey(Const.BuilderParams.ssid)){
            door.setWifiName(jsonObject.getString(Const.BuilderParams.ssid));
        }


        return door;
    }


    public static List<UnitModel> jsonToUnitList(JsonObject jsonObject){

        List<UnitModel> unitList = new ArrayList<>();
        JsonArray jsonArray = jsonObject.getJSONArray(Const.BuilderParams.detail);

        int count =  jsonArray.size();

        if (count > 0 ){

            DataSupport.deleteAll(UnitModel.class);

            for(int i = 0 ;i < count;i++){
                UnitModel unit = jsonToUnit(jsonArray.getJSONObject(i));
                unitList.add(unit);
            }
            //save to db
            DataSupport.saveAll(unitList);
        }

        return unitList;

    }


    private static UnitModel jsonToUnit(JsonObject jsonObject) {

        UnitModel  unit = new UnitModel();
        unit.setUnitId(jsonObject.getLongValue(Const.BuilderParams.unit_id));
        unit.setUnitName(jsonObject.getString(Const.BuilderParams.name));
        unit.setUnitCode(jsonObject.getIntValue(Const.BuilderParams.unit_no));

        return unit;

    }




    public static RequestParams crashDataListToParams(List<CrashData> crashDataList) {
        RequestParams p = new RequestParams();
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(
                crashDataList.size());
        for (int i = 0; i < crashDataList.size(); i++) {
            mapList.add(crashDataToMap(crashDataList.get(i)));
        }
        p.put(Const.BuilderParams.message, Json.toJSONString(mapList));
        return p;
    }

    private static Map<String, Object> crashDataToMap(CrashData crashData) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", crashData.getUserId());
        map.put("login", crashData.getLogin());
        map.put("error_appear_at", DateTool.dateToString(new Date(crashData.getTimestamp()), DateTool.FORMAT_LONG));
        map.put("error_message", crashData.getErrorInfo());
        return map;
    }





    /**
     * Wifi集合转成Json
     * @param data
     * @return
     */
    public static RequestParams  wifiListDataToParams(List<WifiObj> data){

        RequestParams p = new RequestParams();
        int size = data.size();
        List<Map<String, Object>> list = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            Map<String, Object> m = wifiDataToJsonObject(data.get(i));
            list.add(m);
        }
        Map<String, Object> map = new HashMap<>();
        map.put(Const.BuilderParams.REQUEST_WIFI_LIST, list);
        map.put(Const.BuilderParams.door_Bssid, data.get(0).getDoorBssid());

        String json = Json.toJSONString(map);
        p.put(Const.REQUEST_SIGN_IN_RECORD, json);

        return p;

    }

    /**
     * 单个door转成Map
     * @param obj
     * @return
     */
    public static Map<String, Object> wifiDataToJsonObject(WifiObj obj) {
        Map<String, Object> map = new HashMap<>();

        map.put(Const.BuilderParams.REQUEST_WIFI_SSID,obj.getSsid());  //ssid
        map.put(Const.BuilderParams.BSSID,obj.getBssid());  //bssid
        map.put(Const.BuilderParams.REQUEST_SIGNAL,""+obj.getLevel()); //Wifi强度

        return map;
    }


    public static List<Ota>  jsonToOatList(JsonObject jsonObject){

        List<Ota> otaList = new ArrayList<>();

        JsonArray array = jsonObject.getJSONArray(Const.BuilderParams.detail);

        for(int i = 0 ;i < array.size();i++){
            JsonObject obj = array.getJSONObject(i);
            otaList.add(jsonToOat(obj));
        }

        return otaList;
    }


    public static Ota jsonToOat(JsonObject jsonObj){

        Ota ota = new Ota();

        ota.setOtaName(jsonObj.getString(Const.BuilderParams.type));

        ota.setVersion(jsonObj.getString(Const.BuilderParams.version));

        ota.setUrl(jsonObj.getString(Const.BuilderParams.url));

        ota.setMd5(jsonObj.getString(Const.BuilderParams.md5));

        if(Const.CommandParams.Command_AP.equals(ota.getOtaName())){
            ota.setOtaType(OtaType.OTC_AP.type);
        }else{
            ota.setOtaType(OtaType.OTC_PR.type);
        }

        return ota;
    }




}
